package www.ld.com.controller;

import cn.hutool.core.bean.BeanUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import www.ld.com.core.group.InsertGroup;
import www.ld.com.core.group.UpdateGroup;
import www.ld.com.domain.R;
import www.ld.com.domain.post.pojo.PostPO;
import www.ld.com.domain.post.query.PostQuery;
import www.ld.com.domain.post.vo.InPostVO;
import www.ld.com.domain.post.vo.OutPostVO;
import www.ld.com.domain.post.vo.PostVO;
import www.ld.com.domain.vo.PageRestVO;
import www.ld.com.domain.vo.SelectVO;
import www.ld.com.service.PostService;
import www.ld.com.utils.PageUtils;

import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("post")
@Api(tags = "岗位模块")
public class PostController {
    @Autowired
    private PostService postService;

    @ApiOperation(value = "查询岗位列表")
    @GetMapping
    @PreAuthorize("hasAuthority('sys:post:select')")
    public R<PageRestVO<PostVO>> pageList(PostQuery postQuery) {
        PageRestVO pageRestVO = PageUtils.copyPageVO(postService.pagePostByPostQuery(postQuery), PostVO.class);
        return R.ok(pageRestVO);
    }
    @GetMapping("/{id}")
    @ApiOperation(value = "获取岗位详情")
    @PreAuthorize("hasAuthority('sys:post:select')")
    public R<OutPostVO> getPostById(@PathVariable(value="id" ,required = true) String id){
        return R.ok(BeanUtil.toBean(postService.getPostPOById(id), OutPostVO.class));
    }
    @ApiOperation(value = "新增岗位")
    @PostMapping
    @PreAuthorize("hasAuthority('sys:post:create')")
    public R save(@Validated(value = {InsertGroup.class})  InPostVO inPostVO) {
        postService.savePostPO(BeanUtil.toBean(inPostVO, PostPO.class));
        return R.ok();
    }
    @ApiOperation(value = "修改岗位")
    @PutMapping
    @PreAuthorize("hasAuthority('sys:post:update')")
    public R update( @Validated(value = {UpdateGroup.class})  InPostVO inPostVO) {
        postService.updatePostPOById(BeanUtil.toBean(inPostVO, PostPO.class));
        return R.ok();
    }
    @ApiOperation(value = "删除岗位")
    @DeleteMapping("/{id}")
    @PreAuthorize("hasAuthority('sys:post:delete')")
    public R delete(@PathVariable(value = "id", required = true) String id) {
        postService.deletePostById(id);
        return R.ok();
    }

    @ApiOperation(value = "查询岗位")
    @GetMapping("/find")
    public R<List<SelectVO>> findPost() {
        List<PostPO> postPO = postService.findPostPO();
        List<SelectVO>  selects= postPO.stream().map(v ->
            BeanUtil.toBean(v, SelectVO.class)
        ).collect(Collectors.toList());
        return R.ok(selects);
    }

}
